<template>
  <div class="home">
    <el-container>
      <!-- 左边内容 -->
      <el-aside width="auto">
        <div class="logo"></div>
      <el-menu
      unique-opened
      router
      :collapse="iscollapse"
      default-active="2"
      class="el-menu-admin"
      @open="handleOpen"
      @close="handleClose">
      <el-submenu :index="item.path" v-for="item in menusList" :key="item.id">
        <template slot="title">
          <i class="el-icon-location"></i>
          <span>{{item.authName}}</span>
        </template>
        <el-menu-item :index="child.path" v-for="child in item.children" :key="child.id">
          <i class="el-icon-menu"></i>
          <span slot="title">{{child.authName}}</span>
        </el-menu-item>
      </el-submenu>
    </el-menu>
    </el-aside>
    <!-- 右边 -->
      <el-container>
        <!-- 头部 -->
        <el-header>
          <i class="myicon myicon-menu toggle-btn" @click="toggle"></i>
          <div class="system-title">电商后台管理系统</div>
          <div>
            <span class="welcome">您好，{{username}}</span>
             <el-button type="warning" round @click="loginOut">退出</el-button>
          </div>
        </el-header>
        <el-main>
          <!-- 挖坑 -->
          <!-- 给路由添加动画 -->
          <transition
          mode="out-in"
          enter-active-class="animated fadeIn"
          leave-active-class="animated fadeOut"
          >
            <router-view></router-view>
          </transition>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { getMenus } from '@/api'

export default {
  name: 'home',
  components: {

  },
  data () {
    return {
      iscollapse: false, // 控制菜单栏的展开与收缩
      menusList: [],
      // 获取保存的用户名
      username: localStorage.getItem('username')
    }
  },
  mounted () {
    this.initMenus()
  },
  methods: {
    handleOpen (key, keyPath) {
      // console.log(key, keyPath);
    },
    handleClose (key, keyPath) {
      // console.log(key, keyPath);
    },
    // 发送请求
    initMenus () {
      getMenus()
        .then(res => {
          this.menusList = res.data
          // console.log(res)
        })
    },
    // 切换菜单的折叠和展开
    toggle () {
      this.iscollapse = !this.iscollapse
    },
    // 退出功能
    loginOut () {
      // 清除本地存储
      localStorage.clear()
      // 跳转到登录页
      this.$router.push({ name: 'login' })
    }
  }
}
</script>
<style lang="scss" scoped>
.home {
  height: 100%;
  .el-menu-admin:not(.el-menu--collapse) {
    width: 200px;
    min-height: 400px;
  }
  .el-container {
    height: 100%;
  }
  .el-aside {
    background-color: #F9F9F9;
  }
  .el-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #409EFF;
  }
  .logo {
    height:60px;
    background: url(../assets/logo.png);
    background-size: cover;
    background-color: white;
  }
  .toggle-btn {
    padding: 0 15px;
    margin-left: -20px;
    font-size: 36px;
    color: white;
    cursor: pointer;
    line-height: 60px;
    &:hover {
      background-color: #4292CF;
    }
  }
  .system-title {
    font-size: 28px;
    color: white;
  }
  .welcome, {
    color: white;
  }
}
</style>
